﻿Public Class ClsDsAModOneVersion
    'represents cover for aMod version (DsAModOneVersion object)

#Region "Attributes"
#Region "Private attributes"
    Private MeStateOfDS As GeDataSetState = GeDataSetState.kkNotInit  'state of DS
    Private MoDSaModVersion As amod2.DsAModOneVersion = Nothing 'aMod version of DS
    Private MbWasModified As Boolean = True 'indicates if DS was modified since last save    
    Private MbOpenedForReading As Boolean = True 'indicates that version was opened for reading. If true, no check of double opened is performed and no. NO CHECK FOR CHANGE OPERATION IS PERFORMED!!! Its just for info.
#End Region
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property StateOfDS() As GeDataSetState
        Get
            Return MeStateOfDS
        End Get
    End Property
    Public ReadOnly Property DSaModVersion() As amod2.DsAModOneVersion
        Get
            Return MoDSaModVersion
        End Get
    End Property
    Public ReadOnly Property OpenedForReading() As Boolean
        Get
            Return MbOpenedForReading
        End Get
    End Property
    Public Property WasModified() As Boolean
        Get
            Return MbWasModified
        End Get
        Set(ByVal IbValue As Boolean)
            If MbWasModified <> IbValue Then
                MbWasModified = IbValue

                'raise event
                RaiseEvent MMWasChanged()
            End If
        End Set
    End Property
#End Region
#End Region
#Region "Events"
    Public Event MMWasChanged() 'event is raised after value MbWasChanged was changed
#End Region
#Region "Functions"
#Region "Open version, Save version, Close version"
    'opens DS aMod dataset
    Public Function OpenVersion(ByVal IoDsAModOneVersion As AMod2.DsAModOneVersion, ByVal IbOpenedForReading As Boolean) As GeActionResult
        Try
            If IoDsAModOneVersion IsNot Nothing AndAlso MeStateOfDS = GeDataSetState.kkNotInit Then
                MoDSaModVersion = IoDsAModOneVersion
                MbOpenedForReading = IbOpenedForReading
                MeStateOfDS = GeDataSetState.kkOpened
                WasModified = False
                Return GeActionResult.kkOk
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes version
    Public Function CloseVersion() As GeActionResult
        Try
            If MeStateOfDS = GeDataSetState.kkOpened Then
                MeStateOfDS = GeFileState.kkNotInit
                Return GeActionResult.kkOk
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Manipulating with DS"
#Region "INSERT"
    'inserts new unit to DS
    Public Function InsertNewUnit(ByVal IsOriginalUnitName As String, ByVal IsOriginalHumanUnitName As String, ByRef OsNewUnitName As String, ByRef OsNewHumanUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LsNewUnitName As String = MakeNextUniqueUnitName(IsOriginalUnitName)
            Dim LsNewHumanUnitName As String = MakeNextUniqueHumanUnitName(IsOriginalHumanUnitName)
            OsNewUnitName = LsNewUnitName
            OsNewHumanUnitName = LsNewHumanUnitName

            LoRow = MoDSaModVersion.UnitsTable.NewRow()
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName) = LsNewUnitName
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName) = LsNewHumanUnitName

            MoDSaModVersion.UnitsTable.Rows.Add(LoRow)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'inserts new data table to DS
    Public Function InsertNewDataTable(ByVal IsOriginalAppTableName As String, ByVal IsOriginalHumanTableName As String, ByVal IsUnitName As String, ByRef OsNewAppTableName As String, ByRef OsNewHumanTableName As String) As GeActionResult
        Try
            Dim LsNewAppTableName As String = MakeNextUniqueAppTableName(IsOriginalAppTableName)
            Dim LsNewHumanTableName As String = MakeNextUniqueHumanTableName(IsOriginalHumanTableName)
            OsNewAppTableName = LsNewAppTableName
            OsNewHumanTableName = LsNewHumanTableName

            If MoDSaModVersion.AddDataTable(New DataTable(OsNewAppTableName), OsNewAppTableName) IsNot Nothing Then
                Dim LoRow As DataRow = GetTableListRowByAppTableName(OsNewAppTableName)

                'settings for table in TableList
                If LoRow IsNot Nothing Then
                    LoRow(AMod2.DsAModOneVersion.TableSysnames.TableList.Columns.UnitName) = IsUnitName
                    LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.HumanTableName) = OsNewHumanTableName
                    InsertPKcolumnToTable(OsNewAppTableName)
                    MoDSaModVersion.AcceptChanges()
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'inserts new relationship to DS
    Public Function InsertNewRelationshipToHierarchy(ByVal IsParentAppTableName As String, ByVal IsParentHumanTableName As String, ByVal IsChildTableName As String, ByVal IeRelationType As KrosLib.Data.eRelationType) As GeActionResult
        Try
            Dim LsFK As String = MakeUniqueFKForHierarchy(IsChildTableName, IsParentAppTableName)
            Dim LsHumanFK As String = MakeUniqueHumanFKForHierarchy(IsChildTableName, IsParentHumanTableName)

            If InsertFKcolumnToTable(IsChildTableName, LsFK, LsHumanFK) = GeActionResult.kkOk Then
                Dim LoRow As DataRow = GetHierarchyRowFromMainHierarchyByTableName(IsChildTableName)
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ParentTable) = IsParentAppTableName
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ForeignKey) = LsFK
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.RelationType) = IeRelationType
                MoDSaModVersion.AcceptChanges()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert new column to table
    Public Function InsertNewColumnToTable(ByVal IsAppTableName As String, ByVal IsOriginalColumnName As String, ByVal IsOriginalHumanColumnName As String, ByRef OsNewColumnName As String, ByVal IiPositionOfColumn As Integer) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRow = LoTable.NewRow
            SetDefaultSettingsForNewColumn(LoRow)

            Dim LsNewColumnName As String = MakeNextUniqueColumnName(IsAppTableName, IsOriginalColumnName)
            Dim LsNewHumanColumnName As String = MakeNextUniqueHumanColumnName(IsAppTableName, IsOriginalHumanColumnName)

            OsNewColumnName = LsNewColumnName

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = LsNewHumanColumnName
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = LsNewColumnName
            MoDSaModVersion.AcceptChanges()
            LoTable.Rows.Add(LoRow)
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.DefaultValue) = Nothing
            MoDSaModVersion.AcceptChanges()

            'MoDSaModVersion.AddRowToDataTable(LoRow, LoTable)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert PK column to table
    Private Function InsertPKcolumnToTable(ByVal IsAppTableName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRow = LoTable.NewRow
            SetDefaultSettingsForNewColumn(LoRow)

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System) = KrosLib.Data.eSystemColumnType.Id
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = "ID"
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = "ID"
            LoRow(amod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.UsedInView) = False

            LoTable.Rows.Add(LoRow)
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.DefaultValue) = Nothing
            MoDSaModVersion.AcceptChanges()

            'MoDSaModVersion.AddRowToDataTable(LoRow, LoTable)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert FK column to table
    Private Function InsertFKcolumnToTable(ByVal IsAppTableName As String, ByVal IsFK As String, ByVal IsHumanFK As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRow = LoTable.NewRow
            SetDefaultSettingsForNewColumn(LoRow)

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System) = KrosLib.Data.eSystemColumnType.ForeignKey
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = IsHumanFK
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = IsFK
            LoRow(AMod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.UsedInView) = False

            LoTable.Rows.Add(LoRow)
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.DefaultValue) = Nothing
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert new note to DS and return its ID
    Public Function InsertNewNote(ByVal IsNoteText As String, ByRef OiNoteID As Integer) As GeActionResult
        Try
            Dim LiNoteID As Integer = MoDSaModVersion.CreateNewNote(IsNoteText)
            MoDSaModVersion.AcceptChanges()

            OiNoteID = LiNoteID

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "SELECT"
    'gets version (VID)
    Public Function GetVersionVID() As Integer
        Try
            Return CInt(MoDSaModVersion.Tables(KrosLib.Editors.xModDataSet.TableSysnames.Settings.Name).Rows(0).Item(AMod2.DsAModOneVersion.TableSysnames.Settings.Columns.VID))
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return -1
        End Try
    End Function
    'gets table by AppTableName (starts without prefix)
    Public Function GetTableByAppTableName(ByVal IsAppTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataTable
        Try
            Dim LoRows As DataRow() = Nothing
            Dim LsTableName As String = ""

            'I can do it like this:
            'Return MoDSaModVersion.Tables(MakeTableNameFromAppTableName(IsAppTableName))

            'but for sure I select row from TableListTable and exract TableName directly from it
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName, IsAppTableName))

            If LoRows.Length = 1 Then
                LsTableName = LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.TableName)
                'and finaly I can get DataTable from Tables of DS
                Return MoDSaModVersion.Tables(LsTableName)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets table by TableName (starts with prefix "dat_")
    Public Function GetTableByTableName(ByVal IsTableName As String) As DataTable
        Try
            Return MoDSaModVersion.Tables(IsTableName)
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets "column" from data table by column name
    Public Function GetDataTableRowByColumnName(ByVal IsAppTableName As String, ByVal IsColumnName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoTable As DataTable
            Dim LoRows As DataRow()

            LoTable = GetTableByAppTableName(IsAppTableName)

            If LoTable IsNot Nothing Then
                LoRows = LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName, IsColumnName))
                If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                    Return LoRows(0)
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets all datatable rows from tablelist
    Public Function GetAllDataTableRowsFromTableList() As DataRow()
        Try
            Return MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.TableType, CInt(KrosLib.Data.eTableType.DataTable)))

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets "column" from data table by human column name
    Public Function GetDataTableRowByHumanColumnName(ByVal IsAppTableName As String, ByVal IsHumanColumnName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoTable As DataTable
            Dim LoRows As DataRow()

            LoTable = GetTableByAppTableName(IsAppTableName)

            If LoTable IsNot Nothing Then
                LoRows = LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue, IsHumanColumnName))
                If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                    Return LoRows(0)
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets all "columns" from datatable sorted by Position
    Public Function GetDataTableAllRowsSortedByPosition(ByVal IsAppTableName As String) As DataRow()
        Try
            Return GetTableByAppTableName(IsAppTableName).Select("", "Position ASC")
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from TableList by AppTableName
    Public Function GetTableListRowByAppTableName(ByVal IsAppTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName, IsAppTableName))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from notes table by ID
    Public Function GetNoteRowByID(ByVal IiNoteID As Integer, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow() = MoDSaModVersion.NotesTable.Select(String.Format("{0}='{1}'", AMod2.DsAModOneVersion.TableSysnames.Notes.Columns.ID, IiNoteID))
            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets if table has columns joined to any module
    Public Function GetIfTableHasColumnsJoinedToModule(ByVal IsAppTableName As String) As Boolean
        Try
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)
            For Each LoRow As DataRow In LoTable.Rows
                If LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.JoinModuleID) <> "" Then
                    Return True
                    Exit Function
                End If
            Next

            Return False
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return False
        End Try
    End Function
    'gets FK row from table
    Public Function GetFKrowFromTable(ByVal IsAppTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoDataTable As DataTable = GetTableByAppTableName(IsAppTableName)
            Dim LoRows As DataRow()
            LoRows = LoDataTable.Select(String.Format("{0}={1}", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System, CInt(KrosLib.Data.eSystemColumnType.ForeignKey)))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from TableList by HumanTableName
    Public Function GetTableListRowByHumanTableName(ByVal IsHumanTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.HumanTableName, IsHumanTableName))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets child tables of parent table
    Public Function GetChildTablesOfParentTable(ByVal IsParentAppTableName As String) As DataRow()
        Try
            Return MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}'AND {2}='{3}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.HierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.MainHierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ParentTable, IsParentAppTableName))
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets row from unit list table by UnitName
    Public Function GetUnitListRowByUnitName(ByVal IsUnitName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.UnitsTable.Select(String.Format("{0}='{1}'", AMod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName, IsUnitName))

            If LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from unit list table by HumanUnitName
    Public Function GetUnitListRowByHumanUnitName(ByVal IsHumanUnitName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.UnitsTable.Select(String.Format("{0}='{1}'", AMod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName, IsHumanUnitName))

            If LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from hierarchy table which are in _MainHierarchy by TableName
    Public Function GetHierarchyRowFromMainHierarchyByTableName(ByVal IsTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}' AND {2}='{3}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.HierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.MainHierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.TableName, IsTableName))

            If LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets rows of child tables from hierarchy table by parent table name
    Public Function GetChildTablesFromHierarchyFromMainHierarchy(ByVal IsParentTableName As String) As DataRow()
        Try
            Return MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}' AND {2}='{3}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.HierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.MainHierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ParentTable, IsParentTableName))

        Catch ex As Exception
            LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets main hierarchy rows from hiererachy table
    Public Function GetMainHierarchyRowsFromHierarchy()
        Try
            Return MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.HierarchyName, KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.MainHierarchyName))

        Catch ex As Exception
            LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets relationship name
    Public Function GetRelationshipName(ByVal IsChildTableName As String) As String
        Try
            Dim LoRow As DataRow = GetHierarchyRowFromMainHierarchyByTableName(IsChildTableName)
            Return LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RelationName)

        Catch ex As Exception
            LogEventFromException(ex, , )
            Return ""
        End Try
    End Function
    'gets collection of all joined modules from this version
    Public Function GetAllJoinedModulesFromThisVersion() As Collection
        Try
            Dim LoCollection As Collection = New Collection

            For Each LoTableListRow As DataRow In GetAllDataTableRowsFromTableList()
                For Each LoDataTableRow As DataRow In DSaModVersion.Tables(LoTableListRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.TableName)).Rows
                    If LoDataTableRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.JoinModuleID) <> "" Then
                        LoCollection.Add(LoDataTableRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.JoinModuleID))
                    End If
                Next
            Next

            Return LoCollection
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets RTF description of table
    Public Function GetRTFdescriptionOfTable(ByVal IsAppTableName As String) As String
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName, IsAppTableName))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)(AMod2.DsAModOneVersion.TableSysnames.TableList.Columns.RTFDescription)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets RTF description of table column
    Public Function GetRTFdescriptionOfTableRow(ByVal IsAppTableName As String, ByVal IsColumnName As String) As String
        Try
            Dim LoRow As DataRow = GetDataTableRowByColumnName(IsAppTableName, IsColumnName)
            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.RTFDescription)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets RTF description of unit
    Public Function GetRTFdescriptionOfUnit(ByVal IsUnitName As String) As String
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsUnitName)
            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.RTFDescription)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets RTF description of link (relationship) between tables
    Public Function GetRTFdescriptionOfRelationship(ByVal IsChildTableName As String) As String
        Try
            Dim LoRow As DataRow = GetHierarchyRowFromMainHierarchyByTableName(IsChildTableName)
            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RTFDescription)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets class diagram data
    Public Function GetClassDiagramData() As String
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.FlowChartDataTable.Select()

            If LoRows.Length = 1 Then
                Return LoRows(0)(AMod2.DsAModOneVersion.TableSysnames.FlowChart.Columns.ClassDiagram)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets unit ID
    Public Function GetUnitID(ByVal IsUnitName As String) As Integer
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsUnitName)
            Return LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.ID)
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return -1
        End Try
    End Function
    'gets table ID
    Public Function GetTableID(ByVal IsAppTableName As String) As Integer
        Try
            Dim LoRow As DataRow = GetTableListRowByAppTableName(IsAppTableName)
            Return LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.Id)
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return -1
        End Try
    End Function
#End Region
#Region "UPDATE"
    'update class diagram data
    Public Function UpdateClassDiagramData(ByVal IsDiagramData As String) As GeActionResult
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.FlowChartDataTable.Select()

            If LoRows.Length = 1 Then
                LoRows(0)(AMod2.DsAModOneVersion.TableSysnames.FlowChart.Columns.ClassDiagram) = IsDiagramData
            Else
                Dim LoRow As DataRow = MoDSaModVersion.FlowChartDataTable.NewRow
                LoRow(AMod2.DsAModOneVersion.TableSysnames.FlowChart.Columns.ClassDiagram) = IsDiagramData

                MoDSaModVersion.FlowChartDataTable.Rows.Add(LoRow)
            End If
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update table RTF description
    Public Function UpdateRTFDescriptionOfTable(ByVal IsAppTableName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            LoRow = GetTableListRowByAppTableName(IsAppTableName)

            If LoRow IsNot Nothing Then
                LoRow(AMod2.DsAModOneVersion.TableSysnames.TableList.Columns.RTFDescription) = IsRTFtext
                MoDSaModVersion.AcceptChanges()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update table column RTF description
    Public Function UpdateRTFDescriptionOfTableRow(ByVal IsAppTableName As String, ByVal IsColumnName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetDataTableRowByColumnName(IsAppTableName, IsColumnName)

            If LoRow IsNot Nothing Then
                LoRow(AMod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.RTFDescription) = IsRTFtext
                MoDSaModVersion.AcceptChanges()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update unit RTF description
    Public Function UpdateRTFDescriptionOfUnit(ByVal IsUnitName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsUnitName)
            If LoRow IsNot Nothing Then
                LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.RTFDescription) = IsRTFtext
                MoDSaModVersion.AcceptChanges()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update relationship RTF description
    Public Function UpdateRTFDescriptionOfRelationship(ByVal IsChildTableName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetHierarchyRowFromMainHierarchyByTableName(IsChildTableName)
            If LoRow IsNot Nothing Then
                LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RTFDescription) = IsRTFtext
                MoDSaModVersion.AcceptChanges()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update AppTableName
    Public Function UpdateAppTableName(ByVal IsAppTableName As String, ByVal IsNewAppTableName As String) As GeActionResult
        Try
            Dim LeResult As GeActionResult = GeActionResult.kkOk

            Dim LoRow As DataRow = GetTableListRowByAppTableName(IsNewAppTableName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                If MoDSaModVersion.ChangeDataTableName(IsAppTableName, IsNewAppTableName) Then
                    LoRow = GetTableListRowByAppTableName(IsNewAppTableName)
                    LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.SourceTableName) = IsNewAppTableName
                    LeResult = UpdateFKafterParentAppTableNameChanged(IsNewAppTableName)
                Else
                    Throw New Exception
                End If
            End If

            WasModified = True
            Return LeResult
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update HumanTableName
    Public Function UpdateHumanTableName(ByVal IsAppTableName As String, ByVal IsNewHumanTableName As String) As GeActionResult
        Try
            Dim LeResult As GeActionResult = GeActionResult.kkOk

            Dim LoRow As DataRow = GetTableListRowByHumanTableName(IsNewHumanTableName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetTableListRowByAppTableName(IsAppTableName)
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.HumanTableName) = IsNewHumanTableName
                LeResult = UpdateFKafterParentHumanTableNameChanged(IsAppTableName, IsNewHumanTableName)
                MoDSaModVersion.AcceptChanges()
            End If

            WasModified = True
            Return LeResult
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update name of table column
    Public Function UpdateTableColumnName(ByVal IsAppTableName As String, ByVal IsOldTableColumnName As String, ByVal IsNewTableColumnName As String) As GeActionResult
        Try
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)
            Dim LoRow As DataRow = GetDataTableRowByColumnName(IsAppTableName, IsNewTableColumnName, True)

            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetDataTableRowByColumnName(IsAppTableName, IsOldTableColumnName)
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = IsNewTableColumnName
                MoDSaModVersion.AcceptChanges()
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update human name of table column
    Public Function UpdateTableHumanColumnName(ByVal IsAppTableName As String, ByVal IsOldTableHumanColumnName As String, ByVal IsNewTableHumanColumnName As String) As GeActionResult
        Try
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)
            Dim LoRow As DataRow = GetDataTableRowByHumanColumnName(IsAppTableName, IsNewTableHumanColumnName, True)

            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetDataTableRowByHumanColumnName(IsAppTableName, IsOldTableHumanColumnName)
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = IsNewTableHumanColumnName
                MoDSaModVersion.AcceptChanges()
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update - move column in table up
    Public Function UpdateUpColumnPosition(ByVal IsAppTableName As String, ByVal IiRowIndex As Integer) As GeActionResult
        Try
            Dim LoTableRows As DataRow() = GetDataTableAllRowsSortedByPosition(IsAppTableName)
            If IiRowIndex > LoTableRows.Count - 1 Or IiRowIndex - 1 > LoTableRows.Count - 1 Or IiRowIndex < 0 Or IiRowIndex - 1 < 0 Then
                Return GeActionResult.kkCancel
            Else
                Dim LiCurrentRowPosition As Integer = LoTableRows(IiRowIndex)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position)
                Dim LiUpperRowPosition As Integer = LoTableRows(IiRowIndex - 1)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position)

                LoTableRows(IiRowIndex)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position) = LiUpperRowPosition
                LoTableRows(IiRowIndex - 1)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position) = LiCurrentRowPosition

                WasModified = True
                Return GeActionResult.kkOk
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update - move column in table down
    Public Function UpdateDownColumnPosition(ByVal IsAppTableName As String, ByVal IiRowIndex As Integer) As GeActionResult
        Try
            Dim LoTableRows As DataRow() = GetDataTableAllRowsSortedByPosition(IsAppTableName)
            If IiRowIndex > LoTableRows.Count - 1 Or IiRowIndex + 1 > LoTableRows.Count - 1 Or IiRowIndex < 0 Or IiRowIndex + 1 < 0 Then
                Return GeActionResult.kkCancel
            Else
                Dim LiCurrentRowPosition As Integer = LoTableRows(IiRowIndex)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position)
                Dim LiBottomRowPosition As Integer = LoTableRows(IiRowIndex + 1)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position)

                LoTableRows(IiRowIndex)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position) = LiBottomRowPosition
                LoTableRows(IiRowIndex + 1)(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Position) = LiCurrentRowPosition

                WasModified = True
                Return GeActionResult.kkOk
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update UnitName
    Public Function UpdateUnitName(ByVal IsUnitName As String, ByVal IsNewUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsNewUnitName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetUnitListRowByUnitName(IsUnitName)
                LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName) = IsNewUnitName
                MoDSaModVersion.AcceptChanges()
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update HumanUnitName
    Public Function UpdateHumanUnitName(ByVal IsUnitName As String, ByVal IsNewHumanUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetUnitListRowByHumanUnitName(IsNewHumanUnitName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetUnitListRowByUnitName(IsUnitName)
                LoRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName) = IsNewHumanUnitName
                MoDSaModVersion.AcceptChanges()
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update relationship name
    Public Function UpdateRelationshipName(ByVal IsChildTableName As String, ByVal IsRelationshipName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetHierarchyRowFromMainHierarchyByTableName(IsChildTableName)
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RelationName) = IsRelationshipName
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update note text
    Public Function UpdateNoteText(ByVal IiNoteID As Integer, ByVal IsNewText As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetNoteRowByID(IiNoteID)
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Notes.Columns.Text) = IsNewText
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "DELETE"
    'deletes data table from DS
    Public Function DeleteDataTable(ByVal IsAppTableName As String) As GeActionResult
        Try
            MoDSaModVersion.RemoveDataTable(IsAppTableName)
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'delete column from table
    Public Function DeleteColumnFromTable(ByVal IsAppTableName As String, ByVal IsColumnName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetDataTableRowByColumnName(IsAppTableName, IsColumnName)
            If LoRow IsNot Nothing Then
                LoRow.Delete()
                MoDSaModVersion.AcceptChanges()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'deletes Unit from DS
    Public Function DeleteUnit(ByVal IsUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            LoRow = GetUnitListRowByUnitName(IsUnitName)
            LoRow.Delete()
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'deletes relationship from hierarchy (it means changing data in table _Hierarchy)
    Public Function DeleteRelationShipFromHierarchy(ByVal IsParentTableName As String, ByVal IsChildTableName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetHierarchyRowFromMainHierarchyByTableName(IsChildTableName)
            'after changing of ParentTable, xMod DS changes some data in child table so after that Im not able to get FK row, so I need to get it now
            Dim LoFKrow As DataRow = GetFKrowFromTable(IsChildTableName, True)

            'here xMod DS makes some changes!!!
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ParentTable) = GsStringNullValue
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RTFDescription) = GsStringNullValue
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RelationName) = GsStringNullValue
            'foreign key and relation type is set by xMod event so I dont need to set it

            If LoFKrow IsNot Nothing Then LoFKrow.Delete()
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'deletes note from DS
    Public Function DeleteNote(ByVal IiNoteID As Integer) As GeActionResult
        Try
            Dim LoRow As DataRow = GetNoteRowByID(IiNoteID)
            LoRow.Delete()
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "TRIGGERS"
    'update of FK after parent table name was changed
    Private Function UpdateFKafterParentAppTableNameChanged(ByVal IsNewAppTableName As String) As GeActionResult
        Try
            Dim LsNewFK As String = ""
            'ParentTable column was changed from xMod before, so I need only to change FK
            Dim LoChildTablesRows As DataRow() = GetChildTablesOfParentTable(IsNewAppTableName)

            'rows from hierarchy
            Dim LsAppTableName As String = ""
            Dim LoDataTable As DataTable = Nothing
            Dim LoFKrow As DataRow = Nothing
            For Each LoRow As DataRow In LoChildTablesRows
                LsNewFK = MakeUniqueFKForHierarchy(LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName), IsNewAppTableName)
                'change column name in child table
                LsAppTableName = LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.TableName)
                LoDataTable = GetTableByAppTableName(LsAppTableName)
                LoFKrow = GetFKrowFromTable(LsAppTableName)
                LoFKrow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = LsNewFK
                'change FK in hierarchy
                'ATTENTION!!! Here is raised also event in xMod DS and KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System of FK row is set to eSystemColumnType.NotSystemColumn
                'So I need to select this row before this event
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ForeignKey) = LsNewFK
            Next
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update of FK after human parent table name was changed
    Private Function UpdateFKafterParentHumanTableNameChanged(ByVal IsAppTableName As String, ByVal IsNewHumanTableName As String) As GeActionResult
        Try
            Dim LsNewHumanFK As String = ""
            'ParentTable column was changed from xMod before, so I need only to change FK
            Dim LoChildTablesRows As DataRow() = GetChildTablesOfParentTable(IsAppTableName)

            'rows from hierarchy
            Dim LsAppTableName As String = ""
            Dim LoDataTable As DataTable = Nothing
            Dim LoFKrow As DataRow = Nothing
            For Each LoRow As DataRow In LoChildTablesRows
                LsNewHumanFK = MakeUniqueHumanFKForHierarchy(LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName), IsNewHumanTableName)
                'change human column name in child table
                LsAppTableName = LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.TableName)
                LoDataTable = GetTableByAppTableName(LsAppTableName)
                LoFKrow = GetFKrowFromTable(LsAppTableName)
                LoFKrow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = LsNewHumanFK
            Next
            MoDSaModVersion.AcceptChanges()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#End Region
#Region "Support functions"
    'sets default settings for new datatable column
    Private Function SetDefaultSettingsForNewColumn(ByRef IoRow As DataRow) As GeActionResult
        Try
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System) = KrosLib.Data.eSystemColumnType.NotSystemColumn
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.DataType) = KrosLib.Data.eDataType.Int32
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnType) = 0
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Length) = 20
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Decimals) = 0
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.AllowNull) = 0 'pre Romana

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'makes tablename from apptablename
    Public Function MakeTableNameFromAppTableName(ByVal IsAppTableName As String) As String
        ''DOCASNE''
        Return IIf(IsAppTableName <> GsStringNullValue, "dat_" & IsAppTableName, GsStringNullValue)
    End Function
    'makes next unique AppTableName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueAppTableName(ByVal IsOriginalAppTableName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewAppTableName As String = ""

            If GetTableListRowByAppTableName(IsOriginalAppTableName, True) Is Nothing Then
                Return IsOriginalAppTableName
            Else
                '50x checking for unique AppTableName
                For LiI As Integer = 1 To 50
                    LsNewAppTableName = IsOriginalAppTableName & LiIndex.ToString
                    If GetTableListRowByAppTableName(LsNewAppTableName, True) Is Nothing Then Return LsNewAppTableName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalAppTableName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalAppTableName
        End Try
    End Function
    'makes next unique HumanTableName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueHumanTableName(ByVal IsOriginalHumanTableName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewHumanTableName As String = ""

            If GetTableListRowByHumanTableName(IsOriginalHumanTableName, True) Is Nothing Then
                Return IsOriginalHumanTableName
            Else
                '50x checking for unique HumanTableName
                For LiI As Integer = 1 To 50
                    LsNewHumanTableName = IsOriginalHumanTableName & LiIndex.ToString
                    If GetTableListRowByHumanTableName(LsNewHumanTableName, True) Is Nothing Then Return LsNewHumanTableName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalHumanTableName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalHumanTableName
        End Try
    End Function
    'makes next unique UnitName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueUnitName(ByVal IsOriginalUnitName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewUnitName As String = ""

            If GetUnitListRowByUnitName(IsOriginalUnitName, True) Is Nothing Then
                Return IsOriginalUnitName
            Else
                '50x checking for unique UnitName
                For LiI As Integer = 1 To 50
                    LsNewUnitName = IsOriginalUnitName & LiIndex.ToString
                    If GetUnitListRowByUnitName(LsNewUnitName, True) Is Nothing Then Return LsNewUnitName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalUnitName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalUnitName
        End Try
    End Function
    'makes next unique HumanUnitName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueHumanUnitName(ByVal IsOriginalHumanUnitName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewHumanUnitName As String = ""

            If GetUnitListRowByHumanUnitName(IsOriginalHumanUnitName, True) Is Nothing Then
                Return IsOriginalHumanUnitName
            Else
                '50x checking for unique HumanUnitName
                For LiI As Integer = 1 To 50
                    LsNewHumanUnitName = IsOriginalHumanUnitName & LiIndex.ToString
                    If GetUnitListRowByHumanUnitName(LsNewHumanUnitName, True) Is Nothing Then Return LsNewHumanUnitName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalHumanUnitName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalHumanUnitName
        End Try
    End Function
    'makes next unique columnname in mask "BaseNameIndex" - BaseName=string, Index=number
    Public Function MakeNextUniqueColumnName(ByVal IsAppTableName As String, ByVal IsOriginalColumnName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewColumnName As String = ""
            Dim LoRow As DataRow = GetDataTableRowByColumnName(IsAppTableName, IsOriginalColumnName, True) 'LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName, IsOriginalColumnName))

            If LoRow Is Nothing Then
                Return IsOriginalColumnName
            Else
                '50x checking for unique ColumnName
                For LiI As Integer = 1 To 50
                    LsNewColumnName = IsOriginalColumnName & LiIndex.ToString
                    LoRow = GetDataTableRowByColumnName(IsAppTableName, LsNewColumnName, True) 'LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName, LsNewColumnName))
                    If LoRow Is Nothing Then Return LsNewColumnName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalColumnName
        Catch ex As Exception
            'on error return original name
            Return IsOriginalColumnName
        End Try
    End Function
    'makes next unique human columnname in mask "BaseNameIndex" - BaseName=string, Index=number
    Public Function MakeNextUniqueHumanColumnName(ByVal IsAppTableName As String, ByVal IsOriginalHumanColumnName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewHumanColumnName As String = ""
            Dim LoRow As DataRow = GetDataTableRowByHumanColumnName(IsAppTableName, IsOriginalHumanColumnName, True) 'LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue, IsOriginalHumanColumnName))

            If LoRow Is Nothing Then
                Return IsOriginalHumanColumnName
            Else
                '50x checking for unique HumanColumnName
                For LiI As Integer = 1 To 50
                    LsNewHumanColumnName = IsOriginalHumanColumnName & LiIndex.ToString
                    LoRow = GetDataTableRowByHumanColumnName(IsAppTableName, LsNewHumanColumnName, True) 'LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue, LsNewHumanColumnName))
                    If LoRow Is Nothing Then Return LsNewHumanColumnName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalHumanColumnName
        Catch ex As Exception
            'on error return original name
            Return IsOriginalHumanColumnName
        End Try
    End Function
    'makes unique foreign key for hierarchy table - future ColumnName
    Private Function MakeUniqueFKForHierarchy(ByVal IsChildAppTableName As String, ByVal IsParentAppTableName As String) As String
        If IsParentAppTableName = GsStringNullValue Then
            LogEvent("Nazov nadradenej tabulky je """", preto nemozem vytvorit FK!")
            Return ""
        Else
            Return MakeNextUniqueColumnName(IsChildAppTableName, "ID_" & IsParentAppTableName)
        End If
    End Function
    'makes unique human foreign key for hierarchy table - future human column name
    Private Function MakeUniqueHumanFKForHierarchy(ByVal IsChildAppTableName As String, ByVal IsParentHumanTableName As String) As String
        If IsParentHumanTableName = GsStringNullValue Then
            LogEvent("Nazov nadradenej tabulky je """", preto nemozem vytvorit FK!")
            Return ""
        Else
            Return MakeNextUniqueHumanColumnName(IsChildAppTableName, "ID_" & IsParentHumanTableName)
        End If
        Return "Odkaz na " & IsParentHumanTableName
    End Function
#End Region
#End Region

#Region "STARE"
    '' ''    'update - move column down in table _a_Columns
    '' ''    Public Function UpdateDownColumnPosition(ByVal IsAppTableName As String, ByVal IsColumnName As String, ByRef OiNewColumnPosition As Integer) As GeActionResult
    '' ''        Try
    '' ''            Dim LoRows() = GetConcreteColumn(IsAppTableName, IsColumnName)
    '' ''            Dim LiPositionOfChanging As Integer = LoRows(0).Item("Position")

    '' ''            If LiPositionOfChanging < GetNumberOfColumns(IsAppTableName) Then
    '' ''                Dim LoRowsDownColumn() = GetColumnsOfTableWithCondition(IsAppTableName, "Position=" & LiPositionOfChanging + 1)
    '' ''                LoRowsDownColumn(0)("Position") -= 1
    '' ''                LoRows(0)("Position") += 1
    '' ''                OiNewColumnPosition = LoRows(0)("Position")

    '' ''                WasModified = True
    '' ''                Return GeActionResult.kkOk
    '' ''            Else
    '' ''                Debug.Print("CHYBA!!! - UpdateDownColumnPosition")
    '' ''                Return GeActionResult.kkError
    '' ''            End If
    '' ''        Catch ex As Exception
    '' ''            Return GeActionResult.kkError
    '' ''        End Try
    '' ''    End Function
#End Region

End Class